package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author dongdong 抖音号：W13883155245
 * @version 1.0
 * @date 2025/8/13
 * @description 文件上传
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {

    @Autowired
    AliOssUtil ossUtil;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {
        log.info("文件名称：{}", file.getOriginalFilename());
        try {
           //因为没有正确阿里云服务器秘钥，图片上传至本地nginx中
             String filename = file.getOriginalFilename();
            String suffix = filename.substring(filename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replace("-", "");
            filename = uuid + suffix;
            //String filePath = "D:\\workplace\\nginx-1.23.1\\html\\sky\\img\\bucket" + File.separator + filename;
            /*String filePath = "/tmp/dongdongwork/nginx/html/sky/img/bucket" + File.separator + filename;
            log.info("保存路径：{}",filePath);
            file.transferTo(new File(filePath));*/


            // 构建文件保存路径
            String filePath = "/tmp/dongdongwork/nginx/html/admin/img/bucket" + File.separator + filename;
            log.info("保存路径：{}", filePath);

            File destFile = new File(filePath);
            File parentDir = destFile.getParentFile();

            // 检查并创建父目录
            if (parentDir != null && !parentDir.exists()) {
                boolean isDirCreated = parentDir.mkdirs();
                if (!isDirCreated) {
                    log.error("父目录创建失败：{}", parentDir.getAbsolutePath());
                }
            }

            // 检查目录写入权限
            if (parentDir != null && !parentDir.canWrite()) {
                log.error("目录无写入权限：{}", parentDir.getAbsolutePath());
            }

            // 保存文件
            try {
                file.transferTo(destFile);
                log.info("文件保存成功：{}", filePath);
            } catch (IOException e) {
                log.error("文件保存失败", e);

            }


            filePath="https://dongdong.cloud/img/bucket/"+filename;
            log.info("访问路径：{}",filePath);

         /*  //TODO 上传到阿里云服务器
           String orgName = file.getOriginalFilename();
            String extName = orgName.substring(orgName.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String fileName = uuid+extName;
            String filePath = ossUtil.upload(file.getBytes(), fileName);*/
            return Result.success(filePath);
        }catch (Exception e){
            log.error("文件上传失败 {}", e.getMessage());
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }


}
